﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace SusyGame_Model
{
    public class Player
    {
        //Atributos
        private String nickname;
        private int score;
        private int points;

        private List<Defender> defenders = new List<Defender>();

        private MouseState mouseState;
        private MouseState oldState;

        private Map map;

        private Texture2D defenderTexture;

        //Propiedades
        public String Nickname
        {
            get { return nickname; }
            set { nickname = value; }
        }

        public int Score
        {
            get { return score; }
            set { score = value; }
        }
        
        public int Points
        {
            get { return points; }
            set { points = value; }
        }

        public Player(Map map, Texture2D defenderTexture)
        {
            this.map = map;
            this.defenderTexture = defenderTexture;
        }

        private int cellX;
        private int cellY;

        private int tileX;
        private int tileY;

        public void Update(GameTime gameTime, List<Enemy> enemies)
        {
            mouseState = Mouse.GetState();

            cellX = (int)(mouseState.X / 150); 
            cellY = (int)(mouseState.Y / 80);

            tileX = cellX * 150; 
            tileY = cellY * 80;

            if (mouseState.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed)
            {
                if (IsCellClear())
                {
                    Defender defender = new Defender(defenderTexture, new Vector2(tileX, tileY));
                    defenders.Add(defender);
                }
            }

            foreach (Defender defender in defenders)
            {
                if (defender.Target == null)
                {
                    defender.GetClosestEnemy(enemies);
                }

                defender.Update(gameTime);
            }

            oldState = mouseState;
        }

        private bool IsCellClear()
        {
            bool inBounds = cellX >= 0 && cellY >= 0 &&
                cellX < map.Ancho && cellY < map.Alto;

            bool spaceClear = true;

            foreach (Defender defender in defenders) 
            {
                spaceClear = (defender.Position != new Vector2(tileX, tileY));

                if (!spaceClear)
                    break;
            }

            bool onPath = (map.GetIndex(cellX, cellY) == 1);

            return inBounds && spaceClear && onPath; 
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Defender defender in defenders)
            {
                defender.Draw(spriteBatch);
            }
        }
        //Constructores
        //public Player()
        //{ 
        //}

        //public Player(String nickname)
        //{
        //    //Agregamos informacion por defecto
        //    this.Nickname = nickname;
        //    this.score = 0;
        //    this.points = 400;
        //}
    }
}